package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.core.entity.jz.WWarehouseDetail;
import com.sxbbc.common.core.mapper.jz.WWarehouseDetailMapper;
import com.sxbbc.common.core.service.IWWarehouseDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 仓库商品详情表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-02
 */
@Service
public class WWarehouseDetailServiceImpl extends ServiceImpl<WWarehouseDetailMapper, WWarehouseDetail> implements IWWarehouseDetailService {

    @Override
    public boolean updateByWidAndProduct(WWarehouseDetail detail) {
        boolean save = false;
        QueryWrapper<WWarehouseDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("wid",detail.getWid()).eq("pro_code",detail.getProCode()).
                eq("sku_id",detail.getSkuId());
        WWarehouseDetail warehouseDetail = super.getOne(wrapper);
        if (warehouseDetail != null) {
            Integer leftQty = warehouseDetail.getQty();
            if (leftQty >= detail.getQty()) {
                warehouseDetail.setQty(leftQty - detail.getQty());
            }else{
                throw new ThrowJsonException("该仓库库存不足");
            }
            save = super.updateById(warehouseDetail);
            if (!save) {
                throw new ThrowJsonException("更新仓库库存失败");
            }
        }
        return save;
    }

    @Override
    public IPage<WWarehouseDetail> queryProductStock(BasePage<WWarehouseDetail> page, WWarehouseDetail detail) {
        return baseMapper.queryProductStock(page,detail);
    }
}
